<?php
defined('SEJUJOPEFDNGJSL') or exit('Access denied');
function add_data($data=''){
    require_once('../ethereum/dbc.php');
    $db = new Dbc($GLOBALARR['dbhost'], $GLOBALARR['dbuser'],$GLOBALARR['dbpwd'], 'utf8', $GLOBALARR['dbname']);
    return $db->insert($data, 'ld_myzc');
}

function get_all($id=0){
    if($id > 0){
        $sql = 'SELECT id,username,hash,qb_address,out_number,FROM_UNIXTIME(addtime) FROM ld_myzc WHERE id = '.$id;
    }else{
        $sql = 'SELECT id,username,hash,qb_address,out_number,FROM_UNIXTIME(addtime) FROM ld_myzc';
    }
    require_once('../ethereum/dbc.php');
    $db = new Dbc($GLOBALARR['dbhost'], $GLOBALARR['dbuser'],$GLOBALARR['dbpwd'], 'utf8', $GLOBALARR['dbname']);
    $data['data']=$db->select_all($sql);
    $data['count']= count($data['data']);
    return $data;
}


//die(json_encode(add_data()));
if($_POST['qb_address'] != null){
    if($_POST['qb_address']){

        if(!preg_match('/^0x[0-9A-Fa-f]{20}/',trim($_POST['qb_address']))){
            print("<script>(function(){alert('地址有误');location.href='/ucenter/main.php?lsaction=qianbaoinfo'})()</script>");
        }
        if(!trim($_POST['username'])){
            print("<script>(function(){alert('用户名不能为空');location.href='/ucenter/main.php?lsaction=qianbaoinfo'})()</script>");
        }
        if(!floatval($_POST['out_number']>0)){
            print("<script>(function(){alert('数量有误');location.href='/ucenter/main.php?lsaction=qianbaoinfo'})()</script>");
        }
        require_once('../ethereum/Ethereum.php');
        $CoinClient = new EthClient('172.22.108.36', 18554);
        $number = $CoinClient->eth_blockNumber(true);
        if( $number <=0 ){
            exit("钱包对接失败");
        }

        $value = $CoinClient->encode_dec($CoinClient->to_real_value_token(floatval($_POST['out_number']),'2'));
        $tradeInfo = [[
            'from' => '0xb36ebae3d59439f83dbf7ba58e6ff348eab9d855',
            'to' => '0xcf6047999185a7a3f953282b392abbc27d93133b',
            'data' =>  '0xa9059cbb'. $CoinClient->data_pj($_POST['qb_address'], $value),
        ]];
        try{
            $status = $CoinClient->eth_sendTransaction('0xb36ebae3d59439f83dbf7ba58e6ff348eab9d855', 'ljxr#*G^gT9dGrLi37FO&qdTySuc0s', $tradeInfo);
            if($status->error){
                print("<script>(function(){alert('".$status->error->message."');location.href='/ucenter/main.php?lsaction=qianbaoinfo'})()</script>");
            }else{
                $data['username'] = $_POST['username'];
                $data['hash'] = $status->hash;
                $data['qb_address'] = trim($_POST['qb_address']);
                $data['out_number'] = trim($_POST['out_number']);
                $data['addtime'] = time();
                $i=3;
                while($i>0){
                    if(!add_data($data)){
                        $i--;
                    }else{
                        break;
                    }
                }
                print("<script>(
                    function(){
                        alert('转出成功');
                        var coin_obj = Document.getElementById('coin_num');
                        coin_obj.value = '".$str."';
                        location.href='/ucenter/main.php?lsaction=qianbaoinfo'}
                     )()</script>");
            }
        }catch(Exception $e){
            print("<script>(function(){alert('转出失败');location.href='/ucenter/main.php?lsaction=qianbaoinfo'})()</script>");
        }
    }
}

function get_mysql_data($sql){
    //造对象
    $db = new MySQLi("localhost","root","","newssystem");

    //判断是否出错
    if(mysqli_connect_error()){
        return false;
    }
    //执行SQL语句
    $result = $db-> query($sql);
    $v = $result->fetch_row();
    return $v;
}
function admin_main(){
    global $GLOBALARR,$_username;;
    echo '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <title>'.$GLOBALARR['sytitle'].'</title>
    <link rel="stylesheet" type="text/css" href="ucres/css/main.css" />
    <script type="text/javascript" src="ucres/js/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="ucres/art/artDialog.source.js?skin=simple"></script>
    <script type="text/javascript" src="ucres/js/onload.js"></script>
    <script type="text/javascript" src="../ueditor/ueditor.config.js"></script>
    <script type="text/javascript" src="../ueditor/ueditor.all.js"></script>
</head>
<body>
<div class="main-l">
    <div class="menu">
        <div class="menuh clearmenuh"><span>～欢迎您，'.$_username.'！</span></div>
        <div class="menub" style="display:block">
            <a href="#"  title="当前服务器时间">'.date('Y-m-d H:i:s').'</a><br>
            <a href="'.SITEINDEX.'?lsact=index" target="_blank">访问前台</a>
            <a href="'.UCENTERINDEX.'?lsaction=index">后台首页</a><br>
            <a href="index.php?u=1" onclick="return confirm(\'您确定要退出后台吗?\')">退出后台</a><br>
        </div>
    </div>';
    foreach ($GLOBALARR['menu'] as $kr => $vr) {
        if(!strstr($GLOBALARR['adminmenu1'],$kr))   continue;
        echo '<div class="menu"><div class="menuh"><span>'.$kr.'</span></div><div class="menub"><ul>';
        foreach ($vr as $k => $v) {
            if(!strstr($GLOBALARR['adminmenu2'],$v[0])) continue;
            echo '<li><a href="'.$v[1].'">'.$k.'</a></li>';
        }
        echo '</ul></div></div>';
    }

    echo '<br><br></div>

<div class="main-r">';
}


function admin_foot(){
    echo '</div>
</body>
</html>';
}


function admin_one($title,$arr){
    echo '  <h3>'.$title.'</h3>
    <div class="main-r-body">
    <div class="main-r-item">';
    $i=0;
    foreach ($arr as $key => $value) {
        $i++;
        if($i==1) $url=$value;
        echo '<a href="'.$value.'" class="'.geteqval(array($i,1,'quickasel','quicka')).'" name="quickaa" onclick="huana(this)" target="quickiframe">'.$key.'</a>';
    }

    echo '  </div>
    <iframe src="'.$url.'" name="quickiframe" style="width:100%;min-height:570px;margin:0px;padding:0px;"></iframe>
    </div>';
}


function admin_two($arr){
    echo '  <h3>' . $arr['ttitle'] . '</h3>';

    if ( $_GET['lsaction'] =='qianbaoinfo' ){

        require_once('../ethereum/Ethereum.php');
        $CoinClient = new EthClient('172.22.108.36',18554);
        $number = $CoinClient->eth_blockNumber(true);
        if( $number <=0 ){
            exit("钱包对接失败");
        }

        $call = [
            'to' => '0xcf6047999185a7a3f953282b392abbc27d93133b',
            'data' => '0x70a08231'.$CoinClient->data_pj('0xb36ebae3d59439f83dbf7ba58e6ff348eab9d855')
        ];
        $sum = $CoinClient->real_banlance_token($CoinClient->decode_hex($CoinClient->eth_call($call)) , '2');

        echo "<div class=\"main-r-item\"><form method='post' action='/ucenter/main.php?lsaction=qianbaoinfo'> 
            <span class=\"colortxt\" >账户名</span>
            <input type=\"text\" class=\"tsearch\" name='username' value='test' >
            <span class=\"colortxt\" >钱包地址</span>
            <input type=\"hidden\" class=\"tsearch\" name='lsaction' value='qianbaoinfo' >
            <input type=\"text\" class=\"tsearch\" name='qb_address' >
            <span class=\"colortxt\">转出数量</span>
            <input type=\"text\" class=\"tsearch\" name='out_number' >
            <input  type=\"submit\" class=\"thabtn\" value=\"确认\"> 
            <span class=\"colortxt\" >钱包余额:</span>
            <input  type=\"submit\" class=\"\" disabled id='coin_num' value='$sum'> 
            <span class=\"colortxt\" >当前高度:</span>
            <input  type=\"submit\" class=\"\" disabled id='coin_num' value='$number'>
            </form></div>";

        $all_data = get_all();
        $arr['ttr'] = $all_data['data'];
        $str_str = '';
        $arr['tcount'] = $len = $all_data['count'];
        while($len >= 0){
            $len--;
            $str_str .= '<tr>';
            foreach($arr['ttr'][$len-1] as $k => $v) {
                $str_str .= '<td>' . $v . '</td>';
            }
            $str_str .= '</tr>';
        }
        $arr['tth'] = "<th>序号</th><th>用户名</th><th>哈希</th><th>转出地址</th><th>转出数量</th><th>转出时间</th>";
        echo '  </form><form method="post" action="' . geturl() . '">';
        echo '<table class="thealist ttab">
        <tr>
' . $arr['tth'] . '
        </tr>
        <tbody>';
        if ($arr['tcount'] > 0) {
            echo $str_str;
            //$db->query('select * from yang_myzc');
        }
        echo '  </tbody>
    </table></form>';
        if ($arr['tcount'] == 0) {
            echo '<div class="nodata">暂无数据</div>';
        }

        echo '</div>';

    }

    echo '<div class="main-r-body"><form method="get" action="?">
    <div class="main-r-item"><input type="submit" class="thabtn" value="查询"><input type="input" class="tsearch" name="quser"><span class="paixutxt">排序<span>：'.$arr['torderby'];
    if(!empty($arr["twhereoption"])){
        echo '<span class="colortxt">账户类型：<span>'.$arr["twhereoption"];
    }
    if(!empty($arr["tcolor"])){
        echo '<span class="colortxt">颜色：<span>'.$arr['tcolor'];
    }
    if(!empty($arr["tcolorone"])){
        echo '<span class="colortxt">导出：<span>'.$arr['tcolorone'];
    }
    echo '</div>';
    $questr=explode("&",$_SERVER["QUERY_STRING"]);
    for($i=0;$i<count($questr);$i++){
        $questrl=explode("=",$questr[$i]);
        if(count($questrl)>0)
        {
            if($questrl[1]!=""){
                if(!($questrl[0]=="quser")){
                    echo '<input type="hidden" name="'.$questrl[0].'" value="'.$questrl[1].'">';
                }
            }
        }
    }
    echo '  </form><form method="post" action="'.geturl().'">
    <div class="main-r-item" '.$arr["styles"].'>'.$arr['toption'].'</div>   ';

    echo '<table class="thealist ttab">
        <tr>
'.$arr['tth'].'
        </tr>
        <tbody>';
    if($arr['tcount']>0){
        echo $arr['ttr'];
    }
    echo '  </tbody>
    </table></form>';
    if($arr['tcount']==0){
        echo '<div class="nodata">暂无数据</div>';
    }else{
        echo '  <div class="pages">
        <div class="floatr">
'.$arr['tpage'].'
        </div>
    </div>  ';
    }

    echo '</div>';
}


function admin_three($arr){
    echo '  <h3>'.$arr['ttitle'].'</h3>
    <div class="main-r-body">
    <div class="main-r-item">'.$arr['toption'].'</div>';
    echo '<table class="ttab">
        <tr>
'.$arr['tth'].'
        </tr>
        <tbody>';
    if($arr['tcount']>0){
        echo $arr['ttr'];
    }
    echo '  </tbody>
    </table>';
    if($arr['tcount']==0){
        echo '<div class="nodata">暂无数据</div>';
    }
    echo '</div>';
}


function admin_four($arr){
    echo '<div class="main-r-body"><form method="post" action="">
    <div class="main-r-item"><input type="submit" class="thabtn" value="查询"><input type="input" class="tsearch" name="quser"><span class="paixutxt">排序<span>：'.$arr['torderby'];
    echo '</div>';
    $questr=explode("&",$_SERVER["QUERY_STRING"]);
    for($i=0;$i<count($questr);$i++){
        $questrl=explode("=",$questr[$i]);
        if(count($questrl)>0)
        {
            if($questrl[1]!=""){
                if(!($questrl[0]=="quser")){
                    echo '<input type="hidden" name="'.$questrl[0].'" value="'.$questrl[1].'">';
                }
            }
        }
    }
    echo '  </form><form method="post" action="'.geturl().'">
    <div class="main-r-item">'.$arr['toption'].'</div>  ';

    echo '<table class="thealist ttab">
        <tr>
'.$arr['tth'].'
        </tr>
        <tbody>';
    if($arr['tcount']>0){
        echo $arr['ttr'];
    }
    echo '  </tbody>
    </table></form>';
    if($arr['tcount']==0){
        echo '<div class="nodata">暂无数据</div>';
    }else{
        echo '  <div class="pages">
        <div class="floatr">
'.$arr['tpage'].'
        </div>
    </div>  ';
    }

    echo '</div>';
}

function admin_five($arr){
    echo '<div class="main-r-body"><form method="get" action="">
    <div class="main-r-item"><input type="submit" class="thabtn" value="查询"><input type="input" class="tsearch" name="quser"><span class="paixutxt">排序<span>：'.$arr['torderby'].'&nbsp;&nbsp;'.$arr['tttxt'].'</div>';
    $questr=explode("&",$_SERVER["QUERY_STRING"]);
    for($i=0;$i<count($questr);$i++){
        $questrl=explode("=",$questr[$i]);
        if(count($questrl)>0)
        {
            if($questrl[1]!=""){
                if(!($questrl[0]=="quser")){
                    echo '<input type="hidden" name="'.$questrl[0].'" value="'.$questrl[1].'">';
                }
            }
        }
    }
    echo '  </form><form method="post" action="'.geturl().'">';

    echo '<table class="thealist ttab">
        <tr>
'.$arr['tth'].'
        </tr>
        <tbody>';
    if($arr['tcount']>0){
        echo $arr['ttr'];
    }
    echo '  </tbody>
    </table></form>';
    if($arr['tcount']==0){
        echo '<div class="nodata">暂无数据</div>';
    }else{
        echo '  <div class="pages">
        <div class="floatr">
'.$arr['tpage'].'
        </div>
    </div>  ';
    }

    echo '</div>';
}



function admin_six($arr){
    echo '  <h3>'.$arr['ttitle'].'</h3>
    <div class="main-r-body"><form method="get" action="?">
    <div class="main-r-item"><input type="submit" class="thabtn" value="查询"><input type="input" class="tsearch" name="quser"><span class="paixutxt">排序<span>：'.$arr['torderby'].'</div>';
    $questr=explode("&",$_SERVER["QUERY_STRING"]);
    for($i=0;$i<count($questr);$i++){
        $questrl=explode("=",$questr[$i]);
        if(count($questrl)>0)
        {
            if($questrl[1]!=""){
                if(!($questrl[0]=="quser")){
                    echo '<input type="hidden" name="'.$questrl[0].'" value="'.$questrl[1].'">';
                }
            }
        }
    }
    echo '  </form><form method="post" action="'.geturl().'">   ';

    echo '<table class="thealist ttab">
        <tr>
'.$arr['tth'].'
        </tr>
        <tbody>';
    if($arr['tcount']>0){
        echo $arr['ttr'];
    }
    echo '  </tbody>
    </table></form>';
    if($arr['tcount']==0){
        echo '<div class="nodata">暂无数据</div>';
    }else{
        echo '  <div class="pages">
        <div class="floatr">
'.$arr['tpage'].'
        </div>
    </div>  ';
    }

    echo '</div>';
}


function admin_seven($arr){
    echo '  <h3>'.$arr['ttitle'].'</h3>
    <div class="main-r-body"><form method="get" action="?">
    <div class="main-r-item"><input type="submit" class="thabtn" value="查询"><input type="input" class="tsearch" name="quser"><span class="paixutxt">排序<span>：'.$arr['torderby'];
    echo '</div>';
    $questr=explode("&",$_SERVER["QUERY_STRING"]);
    for($i=0;$i<count($questr);$i++){
        $questrl=explode("=",$questr[$i]);
        if(count($questrl)>0)
        {
            if($questrl[1]!=""){
                if(!($questrl[0]=="quser")){
                    echo '<input type="hidden" name="'.$questrl[0].'" value="'.$questrl[1].'">';
                }
            }
        }
    }
    echo '  </form><form method="post" action="'.geturl().'">
    <div class="main-r-all">'.$arr['toption'].'</div>';

    echo '<table class="thealist ttab">
        <tr>
'.$arr['tth'].'
        </tr>
        <tbody>';
    if($arr['tcount']>0){
        echo $arr['ttr'];
    }
    echo '  </tbody>
    </table></form>';
    if($arr['tcount']==0){
        echo '<div class="nodata">暂无数据</div>';
    }else{
        echo '  <div class="pages">
        <div class="floatr">
'.$arr['tpage'].'
        </div>
    </div>  ';
    }

    echo '</div>';
}


function admin_eight($arr){
    echo '  <h3>'.$arr['ttitle'].'</h3><div class="main-r-body">
    <div class="main-r-item">'.$arr['toption'].'</div>  ';

    echo '<table class="thealist ttab">
        <tr>
'.$arr['tth'].'
        </tr>
        <tbody>';
    if($arr['tcount']>0){
        echo $arr['ttr'];
    }
    echo '  </tbody>
    </table></form>';
    if($arr['tcount']==0){
        echo '<div class="nodata">暂无数据</div>';
    }else{
        echo '  <div class="pages">
        <div class="floatr">
'.$arr['tpage'].'
        </div>
    </div>  ';
    }

    echo '</div>';
}
?>
